home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / RLE2GIF.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  8KB  |  235 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to convert RLE (utah raster toolkit) file to GIF format.         *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -c #colors : in power of two, i.e. 7 will allow upto 128 colors in output. *
  10. * -h : on line help.                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 5 Jan 90 - Version 1.0 by Gershon Elber.                     *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <graphics.h>
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #include <io.h>
  21. #include <dos.h>
  22. #include <bios.h>
  23. #endif /* __MSDOS__ */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include "gif_lib.h"
  30. #include "getarg.h"
  31.  
  32. #include "rle.h"               /* The rle tool kit header files. */
  33.  
  34. #define PROGRAM_NAME    "Rle2Gif"
  35.  
  36. #ifdef __MSDOS__
  37. extern unsigned int
  38.     _stklen = 16384;                 /* Increase default stack size. */
  39. #endif /* __MSDOS__ */
  40.  
  41. #ifdef SYSV
  42. static char *VersionStr =
  43.         "Gif library module,\t\tGershon Elber\n\
  44.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  45. static char
  46.     *CtrlStr = "Rle2Gif q%- c%-#Colors!d h%- RleFile!*s";
  47. #else
  48. static char
  49.     *VersionStr =
  50.     PROGRAM_NAME
  51.     GIF_LIB_VERSION
  52.     "    Gershon Elber,    "
  53.     __DATE__ ",   " __TIME__ "\n"
  54.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  55. static char
  56.     *CtrlStr =
  57.     PROGRAM_NAME
  58.     " q%- c%-#Colors!d h%- RleFile!*s";
  59. #endif /* SYSV */
  60.  
  61. /* Make some variables global, so we could access them faster: */
  62. static int
  63.     ColorFlag = FALSE,
  64.     ExpNumOfColors = 8,
  65.     HelpFlag = FALSE,
  66.     ColorMapSize = 256;
  67.  
  68. static void LoadRle(char *FileName,
  69.             GifByteType **RedBuffer,
  70.             GifByteType **GreenBuffer,
  71.             GifByteType **BlueBuffer,
  72.             int *Width, int *Height);
  73. static void SaveGif(GifByteType *OutputBuffer,
  74.             GifColorType *OutputColorMap,
  75.             int ExpColorMapSize, int Width, int Height);
  76. static void QuitGifError(GifFileType *GifFile);
  77.  
  78. /******************************************************************************
  79. * Interpret the command line and scan the given GIF file.              *
  80. ******************************************************************************/
  81. void main(int argc, char **argv)
  82. {
  83.     int    i, j, Error, NumFiles, Width, Height;
  84.     char **FileName = NULL;
  85.     GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL,
  86.         *OutputBuffer = NULL;
  87.     GifColorType *OutputColorMap = NULL;
  88.  
  89.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  90.         &ColorFlag, &ExpNumOfColors, &HelpFlag,
  91.         &NumFiles, &FileName)) != FALSE ||
  92.         (NumFiles > 1 && !HelpFlag)) {
  93.     if (Error)
  94.         GAPrintErrMsg(Error);
  95.     else if (NumFiles > 1)
  96.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  97.     GAPrintHowTo(CtrlStr);
  98.     exit(1);
  99.     }
  100.  
  101.     if (HelpFlag) {
  102.     fprintf(stderr, VersionStr);
  103.     GAPrintHowTo(CtrlStr);
  104.     exit(0);
  105.     }
  106.  
  107.     ColorMapSize = 1 << ExpNumOfColors;
  108.  
  109.     if (NumFiles == 1) {
  110.     LoadRle(*FileName,
  111.         &RedBuffer, &GreenBuffer, &BlueBuffer, &Width, &Height);
  112.     }
  113.     else {
  114.     LoadRle(NULL,
  115.         &RedBuffer, &GreenBuffer, &BlueBuffer, &Width, &Height);
  116.     }
  117.  
  118.     if ((OutputColorMap = (GifColorType *) malloc(ColorMapSize *
  119.                           sizeof(GifColorType))) == NULL ||
  120.     (OutputBuffer = (GifByteType *) malloc(Width * Height *
  121.                         sizeof(GifByteType))) == NULL)
  122.     GIF_EXIT("Failed to allocate memory required, aborted.");
  123.  
  124.     if (QuantizeBuffer(Width, Height, &ColorMapSize,
  125.                RedBuffer, GreenBuffer, BlueBuffer,
  126.                OutputBuffer, OutputColorMap) == GIF_ERROR)
  127.     QuitGifError(NULL);
  128.     free((char *) RedBuffer);
  129.     free((char *) GreenBuffer);
  130.     free((char *) BlueBuffer);
  131.  
  132.     SaveGif(OutputBuffer, OutputColorMap, ExpNumOfColors, Width, Height);
  133. }
  134.  
  135. /******************************************************************************
  136. * Load RLE file into internal frame buffer.                      *
  137. ******************************************************************************/
  138. static void LoadRle(char *FileName,
  139.             GifByteType **RedBuffer,
  140.             GifByteType **GreenBuffer,
  141.             GifByteType **BlueBuffer,
  142.             int *Width, int *Height)
  143. {
  144.     int i, j, k, Size;
  145.     GifByteType *OutputPtr[3];
  146.     rle_hdr in_hdr;
  147.     rle_pixel **rows, *ptr;
  148.     
  149.     if (FileName != NULL) {
  150.     if ((in_hdr.rle_file = fopen(FileName, "r")) == NULL)
  151.         GIF_EXIT("Can't open input file name.");
  152.     }
  153.     else
  154.     in_hdr.rle_file = stdin;
  155.  
  156.     rle_get_setup_ok( &in_hdr, "rle2gif", FileName );
  157.  
  158.     *Width = in_hdr.xmax - in_hdr.xmin + 1;
  159.     *Height = in_hdr.ymax - in_hdr.ymin + 1;
  160.  
  161.     if (in_hdr.ncolors != 3)
  162.     GIF_EXIT("Input Rle file does not hold 3 (RGB) colors, aborted.");
  163.  
  164.     Size = *Width * *Height * sizeof(GifByteType);
  165.     if (rle_row_alloc(&in_hdr, &rows) ||
  166.     (*RedBuffer = (GifByteType *) malloc(Size)) == NULL ||
  167.     (*GreenBuffer = (GifByteType *) malloc(Size)) == NULL ||
  168.     (*BlueBuffer = (GifByteType *) malloc(Size)) == NULL)
  169.     GIF_EXIT("Failed to allocate memory required, aborted.");
  170.  
  171.     OutputPtr[0] = *RedBuffer;
  172.     OutputPtr[1] = *GreenBuffer;
  173.     OutputPtr[2] = *BlueBuffer;
  174.  
  175.     for (i = 0; i < *Height; i++) {
  176.     rle_getrow(&in_hdr, rows);        /* Get one scan line (3 colors). */
  177.  
  178.     for (j = 0; j < 3; j++) { /* Copy the 3 colors to the given buffers. */
  179.         ptr = &rows[j][in_hdr.xmin];
  180.  
  181.         for (k = 0; k < *Width; k++)
  182.         *OutputPtr[j]++ = *ptr++;
  183.     }
  184.     }
  185. }
  186.  
  187. /******************************************************************************
  188. * Save the GIF resulting image.                              *
  189. ******************************************************************************/
  190. static void SaveGif(GifByteType *OutputBuffer,
  191.             GifColorType *OutputColorMap,
  192.             int ExpColorMapSize, int Width, int Height)
  193. {
  194.     int i;
  195.     GifFileType *GifFile;
  196.     GifByteType *Ptr = OutputBuffer + Width * (Height - 1);
  197.  
  198.     /* Open stdout for the output file: */
  199.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  200.     QuitGifError(GifFile);
  201.  
  202.     if (EGifPutScreenDesc(GifFile,
  203.               Width, Height, ExpColorMapSize, 0, ExpColorMapSize,
  204.               OutputColorMap) == GIF_ERROR ||
  205.     EGifPutImageDesc(GifFile,
  206.              0, 0, Width, Height, FALSE, ExpColorMapSize, NULL) ==
  207.                                                                  GIF_ERROR)
  208.     QuitGifError(GifFile);
  209.  
  210.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  211.            PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  212.            GifFile -> IWidth, GifFile -> IHeight);
  213.  
  214.     for (i = 0; i < Height; i++) {
  215.     if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR)
  216.         QuitGifError(GifFile);
  217.     GifQprintf("\b\b\b\b%-4d", Height - i - 1);
  218.  
  219.     Ptr -= Width;
  220.     }
  221.  
  222.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  223.     QuitGifError(GifFile);
  224. }
  225.  
  226. /******************************************************************************
  227. * Close output file (if open), and exit.                      *
  228. ******************************************************************************/
  229. static void QuitGifError(GifFileType *GifFile)
  230. {
  231.     PrintGifError();
  232.     if (GifFile != NULL) EGifCloseFile(GifFile);
  233.     exit(1);
  234. }
  235.